/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myprogramui.comp;

import controller.MyController;
import database.create.CreateMyDatabase;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.TreeView;
import javafx.scene.effect.Bloom;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.util.Duration;
import myfx.jfxpanel.MyMenu;
import myprogramui.comp.page1comp.OpenExcel;
import myprogramui.comp.page1comp.SaveExcel;
import myprogramui.comp.page2comp.RightTree;

/**
 *
 * @author Administrator
 */
public class SettingPage extends Group{
    private Stage primaryStage;
    private Rectangle center;
    private Line line;
    private boolean flag1=false;
    private boolean flag2=true;
    private boolean flag3=true;
    Bloom bloom = new Bloom();
    private RightTree tree;
    private MyController controller;

    public SettingPage(Line line,Rectangle center,Stage primaryStage,final RightTree tree,final MyController controller){
        this.controller=controller;
        this.tree=tree;
        bloom.setThreshold(0.1);
        this.primaryStage=primaryStage;
        this.center=center;
        this.line=line;
        ImageView iv_page1=new ImageView("Image_no_pair/page1/header.png");
        this.getChildren().add(iv_page1);
        iv_page1.setTranslateX(250);
        iv_page1.setTranslateY(140);
        iv_page1.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(85));
        
        //吊环，10个
        ImageView iv_diaohuan_1=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_2=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_3=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_4=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_5=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_6=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_7=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_8=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_9=new ImageView("Image_no_pair/page1/diaohuan.png");
        ImageView iv_diaohuan_10=new ImageView("Image_no_pair/page1/diaohuan.png");
        //底部
        ImageView iv_bottom_1=new ImageView("Image_no_pair/page1/bottom_1.png");
        ImageView iv_bottom_2=new ImageView("Image_no_pair/page1/bottom_1.png");
        ImageView iv_bottom_3=new ImageView("Image_no_pair/page1/bottom_1.png");
        ImageView iv_bottom_4=new ImageView("Image_no_pair/page1/bottom_1.png");
        
        Rectangle container=RectangleBuilder.create()
                .arcHeight(10)
                .arcWidth(10)
                .width(1024)
                .height(250)
                .stroke(Color.WHITE)
                .fill(Color.WHITE)
                .y(140+233+10)
                .build();
        container.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(85));
        this.getChildren().add(container);
        
        iv_diaohuan_1.setTranslateY(140+212);
        iv_diaohuan_1.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87));
        this.getChildren().add(iv_diaohuan_1);
        iv_diaohuan_2.setTranslateY(140+212);
        iv_diaohuan_2.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+111));
        this.getChildren().add(iv_diaohuan_2);
        iv_diaohuan_3.setTranslateY(140+212);
        iv_diaohuan_3.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+222));
        this.getChildren().add(iv_diaohuan_3);
        iv_diaohuan_4.setTranslateY(140+212);
        iv_diaohuan_4.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+333));
        this.getChildren().add(iv_diaohuan_4);
        iv_diaohuan_5.setTranslateY(140+212);
        iv_diaohuan_5.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+444));
        this.getChildren().add(iv_diaohuan_5);
        iv_diaohuan_6.setTranslateY(140+212);
        iv_diaohuan_6.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+555));
        this.getChildren().add(iv_diaohuan_6);
        iv_diaohuan_7.setTranslateY(140+212);
        iv_diaohuan_7.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+666));
        this.getChildren().add(iv_diaohuan_7);
        iv_diaohuan_8.setTranslateY(140+212);
        iv_diaohuan_8.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+777));
        this.getChildren().add(iv_diaohuan_8);
        iv_diaohuan_9.setTranslateY(140+212);
        iv_diaohuan_9.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+888));
        this.getChildren().add(iv_diaohuan_9);
        iv_diaohuan_10.setTranslateY(140+212);
        iv_diaohuan_10.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(87+999));
        this.getChildren().add(iv_diaohuan_10);
        
        //增加底部
        iv_bottom_1.setTranslateY(140+233+10+215);
        iv_bottom_1.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+78));
        this.getChildren().add(iv_bottom_1);
        iv_bottom_2.setTranslateY(140+233+10+215);
        iv_bottom_2.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+78+250));
        this.getChildren().add(iv_bottom_2);
        iv_bottom_3.setTranslateY(140+233+10+215);
        iv_bottom_3.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+78+500));
        this.getChildren().add(iv_bottom_3);
        iv_bottom_4.setTranslateY(140+233+10+215);
        iv_bottom_4.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+78+750));
        this.getChildren().add(iv_bottom_4);
        
        //四个主要的rectangle
        Rectangle but_open=RectangleBuilder.create()
                .arcHeight(10)
                .arcWidth(10)
                .width(180)
                .height(180)
                .fill(Color.rgb(166, 137, 124))
                .build();
        Rectangle but_save=RectangleBuilder.create()
                .arcHeight(10)
                .arcWidth(10)
                .width(180)
                .height(180)
                .fill(Color.rgb(166, 137, 124))
                .build();
        Rectangle but_setting=RectangleBuilder.create()
                .arcHeight(10)
                .arcWidth(10)
                .width(180)
                .height(180)
                .fill(Color.rgb(166, 137, 124))
                .build();
        Rectangle but_other=RectangleBuilder.create()
                .arcHeight(10)
                .arcWidth(10)
                .width(180)
                .height(180)
                .fill(Color.rgb(166, 137, 124))
                .build();
        
        but_open.setTranslateY(140+233+10+30);
        but_open.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+40));
        this.getChildren().add(but_open);
        but_save.setTranslateY(140+233+10+30);
        but_save.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+40+250));
        this.getChildren().add(but_save);
        but_setting.setTranslateY(140+233+10+30);
        but_setting.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+40+500));
        this.getChildren().add(but_setting);
        but_other.setTranslateY(140+233+10+30);
        but_other.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+40+750));
        this.getChildren().add(but_other);
        //四个buttonImage
        final ImageView iv_but_open=new ImageView("Image_no_pair/page1/open1.png");
        iv_but_open.setTranslateY(140+233+10+40);
        iv_but_open.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+60));
        this.getChildren().add(iv_but_open);
        final ImageView iv_but_save=new ImageView("Image_no_pair/page1/lingcunwei.png");
        iv_but_save.setTranslateY(140+233+10+40);
        iv_but_save.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+60+250));
        this.getChildren().add(iv_but_save);
        final ImageView iv_but_setting=new ImageView("Image_no_pair/page1/allseting.png");
        iv_but_setting.setTranslateY(140+233+10+40);
        iv_but_setting.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+60+500));
        this.getChildren().add(iv_but_setting);
        final ImageView iv_but_other=new ImageView("Image_no_pair/page1/open1.png");
        iv_but_other.setTranslateY(140+233+10+40);
        iv_but_other.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+60+750));
        this.getChildren().add(iv_but_other);
        //四个文本
        Text txt_open=TextBuilder.create()
                .stroke(Color.BLACK)
                .strokeWidth(2)
                .font(new Font("黑体", 20))
                .text("打开文件")
                .effect(bloom)
                .build();
        Text txt_save=TextBuilder.create()
                .stroke(Color.BLACK)
                .strokeWidth(2)
                .font(new Font("黑体", 20))
                .text("导出文件")
                .effect(bloom)
                .build();
        Text txt_setting=TextBuilder.create()
                .stroke(Color.BLACK)
                .strokeWidth(2)
                .font(new Font("黑体", 20))
                .text("工作设定")
                .effect(bloom)
                .build();
        Text txt_other=TextBuilder.create()
                .stroke(Color.BLACK)
                .strokeWidth(2)
                .font(new Font("黑体", 20))
                .text("附加设定")
                .effect(bloom)
                .build();
        txt_open.setTranslateY(140+233+10+240);
        txt_open.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90));
        this.getChildren().add(txt_open);
        txt_save.setTranslateY(140+233+10+240);
        txt_save.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90+250));
        this.getChildren().add(txt_save);
        txt_setting.setTranslateY(140+233+10+240);
        txt_setting.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90+500));
        this.getChildren().add(txt_setting);
        txt_other.setTranslateY(140+233+10+240);
        txt_other.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90+750));
        this.getChildren().add(txt_other);
        //一个显示的rectangle背景
        final Rectangle show_back=RectangleBuilder.create()
                .arcHeight(20)
                .arcWidth(20)
                .width(210)
                .height(210)
                .fill(Color.WHITE)
                .stroke(Color.CADETBLUE)
                .strokeWidth(1)
                .build();
        show_back.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90+640));
        show_back.setTranslateY(120);
        this.getChildren().add(show_back);
        final ImageView iv_show_pos=new ImageView();
        iv_show_pos.translateXProperty().bind(primaryStage.widthProperty().divide(8).add(95+90+670));
        iv_show_pos.setTranslateY(160);
        this.getChildren().add(iv_show_pos);
        //四个指向标
        final ImageView paperPlane=new ImageView("Image_no_pair/page1/plane.png");
        paperPlane.setTranslateX(320);
        paperPlane.setTranslateY(540);
        final ImageView paperPlane1=new ImageView("Image_no_pair/page1/plane.png");
        paperPlane1.setTranslateX(320+250);
        paperPlane1.setTranslateY(540);
        final ImageView paperPlane2=new ImageView("Image_no_pair/page1/plane.png");
        paperPlane2.setTranslateX(320+500);
        paperPlane2.setTranslateY(540);
        final ImageView paperPlane3=new ImageView("Image_no_pair/page1/plane.png");
        paperPlane3.setTranslateX(320+750);
        paperPlane3.setTranslateY(540);
        
        this.getChildren().add(paperPlane);
        this.getChildren().add(paperPlane1);
        this.getChildren().add(paperPlane2);
        this.getChildren().add(paperPlane3);
        //设定变换
        
        final TranslateTransition tt = new TranslateTransition(Duration.millis(300), paperPlane);
        tt.setCycleCount(1);
        tt.setAutoReverse(true);
        final TranslateTransition tt_back = new TranslateTransition(Duration.millis(300), paperPlane);
        tt.setCycleCount(1);
        tt.setAutoReverse(true);
        final SequentialTransition st =new SequentialTransition();
        st.getChildren().addAll(tt,tt_back);
        //button 事件处理
        iv_but_open.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                
                tt.setNode(paperPlane);
                tt_back.setNode(paperPlane);
                tt.setByX(show_back.getTranslateX()-iv_but_open.getTranslateX());
                tt.setByY(show_back.getTranslateY()-iv_but_open.getTranslateY());
                tt_back.setByX(-show_back.getTranslateX()+iv_but_open.getTranslateX());
                tt_back.setByY(-show_back.getTranslateY()+iv_but_open.getTranslateY());
                st.play();
                show_back.setFill(Color.FLORALWHITE);
                iv_show_pos.setImage(iv_but_open.getImage());
                //这里添加打开事件
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("选择你要导入的Excel文件,格式为xls或xlsx");
                    fileChooser.getExtensionFilters().add(
                            new FileChooser.ExtensionFilter("Excel all format", "*.xls","*.xlsx"));
                    File selectedFile = fileChooser.showOpenDialog(null);
                    if(selectedFile!=null){                        
                        
                        OpenExcel oe=new OpenExcel(selectedFile.getAbsolutePath(), tree,controller);
                        oe.setFilepath(selectedFile.getAbsolutePath());
                        oe.doLoad();
                    }
                    
                } catch (Exception ex) {
                    System.out.println("SettingPage导入文件失败");
                    ex.printStackTrace();
                }
            }
        });
        iv_but_save.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                tt.setNode(paperPlane1);
                tt_back.setNode(paperPlane1);
                tt.setByX(show_back.getTranslateX()-iv_but_save.getTranslateX());
                tt.setByY(show_back.getTranslateY()-iv_but_save.getTranslateY());
                tt_back.setByX(-show_back.getTranslateX()+iv_but_save.getTranslateX());
                tt_back.setByY(-show_back.getTranslateY()+iv_but_save.getTranslateY());
                st.play();
                show_back.setFill(Color.YELLOWGREEN);
                iv_show_pos.setImage(iv_but_save.getImage());
                try {
                    FileChooser fileChooser = new FileChooser();
                    fileChooser.setTitle("3D数据库文件另存为");
                    fileChooser.getExtensionFilters().add(
                            new FileChooser.ExtensionFilter("Microsoft stylesheet file", "*.xlsx"));
                    File outFile = fileChooser.showSaveDialog(null);
                    if (outFile != null) {                       
                        String extName = outFile.getAbsoluteFile().toString().substring(
                                outFile.getAbsoluteFile().toString().lastIndexOf(".") + 1);
                        
                        if(extName.equals("xlsx")){
                            SaveExcel se=new SaveExcel(outFile.getAbsolutePath());
                            se.setFilepath(outFile.getAbsolutePath());
                            se.addAll();
                            se.saveData();
                        }                     
                        
                    }
                    
                } catch (Exception ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        iv_but_setting.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                tt.setNode(paperPlane2);
                tt_back.setNode(paperPlane2);
                tt.setByX(show_back.getTranslateX()-iv_but_setting.getTranslateX());
                tt.setByY(show_back.getTranslateY()-iv_but_setting.getTranslateY());
                tt_back.setByX(-show_back.getTranslateX()+iv_but_setting.getTranslateX());
                tt_back.setByY(-show_back.getTranslateY()+iv_but_setting.getTranslateY());
                st.play();
                show_back.setFill(Color.PINK);
                iv_show_pos.setImage(iv_but_setting.getImage());
            }
        });
        iv_but_other.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                tt.setNode(paperPlane3);
                tt_back.setNode(paperPlane3);
                tt.setByX(show_back.getTranslateX()-iv_but_other.getTranslateX());
                tt.setByY(show_back.getTranslateY()-iv_but_other.getTranslateY());
                tt_back.setByX(-show_back.getTranslateX()+iv_but_other.getTranslateX());
                tt_back.setByY(-show_back.getTranslateY()+iv_but_other.getTranslateY());
                st.play();
                show_back.setFill(Color.TURQUOISE);
                iv_show_pos.setImage(iv_but_other.getImage());
            }
        });
        

    }
}
